﻿-- ===============================================================================================
-- Basic View for Terminal
-- ===============================================================================================

IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[esf_sso].[VTerminal]'))
	DROP VIEW [esf_sso].[VTerminal]
GO

-- SELECT * FROM [esf_sso].[VTerminal]

CREATE VIEW [esf_sso].[VTerminal] AS

	SELECT 
		VBR.*

		,VE.Ent_ID							Ter_Ent_ID
		,VE.Ent_Name						Ter_Ent_Name
		,VE.Ent_IdentificationNumber		Ter_Ent_IdentificationNumber
		,VE.Ent_IdentificationNumberDigit	Ter_Ent_IdentificationNumberDigit
	FROM 
		esf_sso.VLocatedBasicReference VBR
	-- Terminales de la empresa del usuario
	LEFT JOIN
		esf_sso.TreeBasicReference TxE ON TxE.BasicReferenceID = VBR.ID
	LEFT JOIN
		esf_sso.[VEnterprise] VE ON VE.Ent_ID = TxE.ParentBasicReferenceID
	WHERE 
		VBR.BasicReferenceTypeID = 11
GO